package com.yincai.carefreewater.tool;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by ${zhangke} on 2017/6/9.
 *
 * Java一些方法封装
 */

public class JavaTool {

    /**
     * @param number
     * @return
     *
     * 转int
     */
    public  static  int getInteger(String number){

        try {
           return Integer.parseInt(number);
        } catch (NumberFormatException e) {
            return -1;
        }

    }

    /**

     * @param number
     * @return
     *
     * 转doulbe
     */
    public  static  double getDouble(String number){

        try {
           return Double.parseDouble(number);
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    /**
     * @param d
     * @return
     *
     * 保留两位小数
     */
    public static String KeepTwo(double d){

        DecimalFormat df   = new DecimalFormat("######0.00");

        String format = df.format(d);


        return format;

    }

    /**
     * @param context
     * @param mPermissions
     *
     * 动态申请权限
     */
    public  static  void  getPermission (Context context, List<String> mPermissions){

        if (Build.VERSION.SDK_INT>=23){

            if (mPermissions.size() > 0) {
                ((Activity)context).requestPermissions((String[]) mPermissions.toArray(new String[mPermissions.size()]), 111);
            }

        }
    }

    /**
     * @param context
     * @param permissions
     *  动态权限集合
     */
    public  static  void  getPermissionList (Context context, String permissions, List<String> mPermissions){
        if (Build.VERSION.SDK_INT>=23){

            if (((Activity)context).checkSelfPermission(permissions)!= PackageManager.PERMISSION_GRANTED){
                mPermissions.add(permissions);
            }

        }
    }
}
